package com.example.demo.zip.test;

import com.example.demo.zip.service.ProductService;

/**
 * @author zjq
 * @version 创建时间：2022-4-6 16:39:42
 */
public class ProductController {
    // 看到的某一个案例--没用上
    ProductService productService;

//	// JAVA中批量下载文件，将下载多个文件打包成zip文件下载。
//	// 批量文件下载(将多个文件打包成zip包下载)
//	@RequestMapping(value = "/downloadzipss/{attachIds}", method = { RequestMethod.POST, RequestMethod.GET })
//	public void batchDownLoadFile(@PathVariable String attachIds, HttpServletRequest request,
//			HttpServletResponse response, String filename, String[] filepath, String[] documentname, String loginname) {
//
//		List<String> attachId = Arrays.asList(attachIds.split(","));
//		List<Long> attachs = new ArrayList<Long>();
//		attachId.forEach(it -> {
//			attachs.add(Long.valueOf(it));
//		});
//		List<CuxFndAttachmentDomain> cuxFndAttachmentList = cuxFndAttachmentService.findAttachmentByIds(attachs);
//		byte[] buffer = new byte[1024];
//		Date date = new Date();
//		// 生成zip文件存放位置
//		String path = diskPath;
//		String strZipPath = diskPath + "/" + date.getTime() + ".zip";
//		File file = new File(diskPath);
//		if (!file.isDirectory() && !file.exists()) {
//			// 创建单层目录
//			// f.mkdir();
//			// 创建多层目录
//			file.mkdirs();
//		}
//		try {
//			ZipOutputStream out = new ZipOutputStream(new FileOutputStream(strZipPath));
//			for (CuxFndAttachment attach : cuxFndAttachmentList) {
//				String storageDomain = attach.getAttachStorageDomain();
//				String fileName = attach.getAttachName();
//				if (ZHIYUAN_OA.equals(storageDomain)) {
//					String attachZhiyuanOaId = attach.getAttachZhiyuanOaId();
//					InputStream inputStream = FlowFileUpDown.FileDw(attachZhiyuanOaId);
//					out.putNextEntry(new ZipEntry(fileName));
//					// 设置压缩文件内的字符编码，不然会变成乱码
//					out.setEncoding("GBK");
//					int len;
//					// 读入需要下载的文件的内容，打包到zip文件
//					while ((len = inputStream.read(buffer)) > 0) {
//						out.write(buffer, 0, len);
//					}
//					out.closeEntry();
//				}
//			}
//			out.close();
//			// downLoadFile(request, response, strZipPath, strZipPath);
//			// 5.设置下载下载头
//			SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
//			String filenames = URLEncoder.encode("批量下载文件" + format.format(Calendar.getInstance().getTime()) + ".zip",
//					"UTF-8");
//			response.setHeader("Content-Disposition", "attachment; fileName=" + filenames);
//			response.setContentType("application/octet-stream; charset=utf-8");
//			FileInputStream fileInputStream = null;
//			fileInputStream = new FileInputStream(strZipPath);
//			// 下载文件
//			FileCopyUtils.copy(fileInputStream, response.getOutputStream());
//			File temp = new File(strZipPath);
//			if (temp.exists()) {
//				temp.delete();
//			}
//		} catch (Exception e) {
//			System.out.println("文件下载错误");
//		}
//	}
}
